package com.itheima.controller;


import cn.itheima.health.resources.UploadUtils;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@RequestMapping("upload")
public class UploadController {

    @RequestMapping("upload.do")
    public  String  upload(@RequestParam("upload") MultipartFile file){
           //   file  通过jersey  传递  9001  服务器 uploads下即可
        try {
            String  uuidfilename = UploadUtils.generateRandonFileName(file.getOriginalFilename());
            Client  client = new Client();
            WebResource resource = client.resource("http://127.0.0.1:9001/uploads/" + uuidfilename);
            resource.put(file.getBytes());
            return "redirect:/ok.html";
        } catch (IOException e) {
            e.printStackTrace();
            return "redirect:/error.html";
        }


    }
}
